/*
 * description: 通过扫描方式加载服务
 */
const files = require.context('@/api/', true, /\.js$/);
const ignore = ['./request.js', './demo.js'];
const api = {};

files
    .keys()
    .filter(e => !ignore.includes(e))
    .map(key => {
        if (key.includes('--ignore')) {
            return false;
        }
        if (key.split('/').length !== 3) {
            Promise.reject('file error' + key);
            return;
        }
        const item = files(key).default;

        let keySplit = key
            .split('/')
            .filter(item => {
                return item.indexOf('.js') !== -1;
            })[0]
            .replace('.js', '');

        let ObjectName = key.split('/')[1];
        try {
            if (api[ObjectName]) {
                api[ObjectName][keySplit] = new item();
            } else {
                api[ObjectName] = {};
                api[ObjectName][keySplit] = new item();
            }
        } catch {
            Promise.reject('file error' + key);
        }
    });

export default api;
